package com.tuling.jucdemo.sync;

import org.openjdk.jol.info.ClassLayout;

/**
 * @author :tangweiliang
 * @date : 2022/11/10 20:14
 * 未来的你会感谢现在努力的你
 *
 *关闭延迟开启偏向锁:‐XX:BiasedLockingStartupDelay=0
 *
 */
public class ObjectTest {
    public static void main(String[] args) throws InterruptedException {
        // jvm延迟偏向
        Thread.sleep(5000);
        //Object object = new Object();
        Object object = new Test();
        //object.hashCode();// 无锁状态 01  ThreadA线程变成轻量级锁 00
        //  查看对象内部信息
        System.out.println(ClassLayout.parseInstance(object).toPrintable()); // 16 24

        new Thread(()->{
            synchronized (object){ //ObjectTest.class 去掉参数 ‐XX:BiasedLockingStartupDelay=0  会变成轻量级锁 00
                //object.hashCode();//会变成重量级锁 10
               // object.notify();//会变成轻量级锁 00
//                try {
//                    object.wait(100); //会变成重量级锁 10
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                System.out.println(Thread.currentThread().getName()+"\n"+ClassLayout.parseInstance(object).toPrintable());
            }
            System.out.println(Thread.currentThread().getName()+"释放锁\n"+ClassLayout.parseInstance(object).toPrintable());
            // JVM优化
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"ThreadA").start(); //应该是重量级锁10 可是却变成了00

        Thread.sleep(2000);

        new Thread(()->{
            synchronized (object){
                System.out.println(Thread.currentThread().getName()+"\n"+ClassLayout.parseInstance(object).toPrintable());
            }
        },"ThreadB").start();
    }
}

class Test{
    private boolean f;
    private long p;
}